package cn.edu.scau.cmi.yangjiamin.client;

import cn.edu.scau.cmi.yangjiamin.hibernate.Student;
import cn.edu.scau.cmi.yangjiamin.hibernate.Teacher;
import cn.edu.scau.cmi.yangjiamin.hibernate.StudentDao;
import cn.edu.scau.cmi.yangjiamin.hibernate.TeacherDao;

import java.util.ArrayList;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class HibernateClient extends Application {
	@Override
	public void start(Stage primaryStage) {
		
		TeacherDao teaDao = new TeacherDao();
		
		StudentDao stuDao = new StudentDao();
	    
		//先清除数据库内容
        for(int i = 0; i < 100; i++) {
        	teaDao.deleteTea(i);
        	stuDao.deleteStu(i);
        }
		

		BorderPane pane = new BorderPane();
		HBox hbox = new HBox();
		hbox.setPadding(new Insets(10, 10, 10, 10));
		Button newTea = new Button("新建教师");
		Button newStu = new Button("新建学生");
        
		Button showTea = new Button("展示所有教师信息");
		Button showStu = new Button("展示所有学生信息");

		VBox vbox = new VBox(15);
		vbox.setPadding(new Insets(5, 5, 5, 5));

		Label labelStu = new Label("请先输入学生姓名(新建学生时):");
		TextField tfStu = new TextField();
		Label labelTea = new Label("请输入教师姓名:");
		TextField tfTea = new TextField();
		vbox.getChildren().addAll(labelStu, tfStu, labelTea, tfTea);

		hbox.getChildren().addAll(newTea, newStu, showTea, showStu);
		Text text = new Text(100, 100, "显示结果");

		newTea.setOnAction(e -> {
			tfTea.setText("");
			tfTea.setOnAction(e1 -> {
				teaDao.addTea(tfTea.getText());
				text.setText("新建教师" + tfTea.getText() + "成功");
			});
		});

		newStu.setOnAction(e -> {
			tfStu.setText("");
			tfTea.setText("");
			tfStu.setOnAction(e1 -> {//先输入学生姓名
				stuDao.addStu(tfStu.getText());
				String stu_name = tfStu.getText();
				text.setText("新建学生" + tfStu.getText() + "成功");
				tfTea.setOnAction(e2 -> {//再输入教师姓名
					String tea_name = tfTea.getText();
					Integer tea_id = null;
					Integer stu_id = null;
					System.out.println(tea_name + " " + stu_name);
					ArrayList<Student> stuList = stuDao.getStuList();
					for (Student stu : stuList) {
						if (stu.getStu_name().equals(stu_name)) {
							stu_id = stu.getStu_id();
							break;
						}
					}

					ArrayList<Teacher> teaList = teaDao.getTeaList();
					for (Teacher tea : teaList) {
						if (tea.getTea_name().equals(tea_name)) {
							tea_id = tea.getTea_id();
							break;
						}
					}
					stuDao.addTea(stu_id, tea_id);
					text.setText("为学生" + tfStu.getText() + "添加指导教师" + tfTea.getText() + "成功");
				});
			});

		});

		showTea.setOnAction(e -> {
			tfTea.setText("");
			tfStu.setText("");
			StringBuilder sb = new StringBuilder();
			ArrayList<Teacher> teaList = teaDao.getTeaList();
			for (Teacher tea : teaList) {
				sb.append("工号: " + tea.getTea_id() + " 教师姓名: " + tea.getTea_name() + "\n");
			}
			text.setText(sb.toString());

		});

		showStu.setOnAction(e -> {
			tfTea.setText("");
			tfStu.setText("");
			StringBuilder sb = new StringBuilder();
			ArrayList<Student> stuList = stuDao.getStuList();
			for (Student stu : stuList) {
				sb.append("学号: " + stu.getStu_id() + " 学生姓名: " + stu.getStu_name());
				if(stu.getTea()!=null) {
					sb.append(" 指导教师: " + stu.getTea().getTea_name() + "\n");
				}
				else {
					sb.append(" 指导教师: 无"  + "\n");
				}
			}
			text.setText(sb.toString());

		});

		pane.setTop(hbox);
		pane.setLeft(vbox);
		pane.setCenter(text);
		Scene scene = new Scene(pane, 600, 500);
		primaryStage.setTitle("HibernateClient");
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}

